/*
 * The Log
 */
package config;

import java.awt.Color;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JTextArea;

/**
 * The log Dialog
 * @author Kay van Bree
 * @version 0.4
 */
public class Log extends JDialog {
	private TextArea textarea;
	private static Log log;
	private JCheckBoxMenuItem menuItem;
	
	/**
	 * Creates a Log
	 * @author Kay van Bree
	 * @since 0.4
	 */
	private Log(){
        setSize(400, 310);
		getContentPane().setBackground(Color.lightGray);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		this.setLocationByPlatform(true);
		this.setLocation(800, 200);
		menuItem = new JCheckBoxMenuItem("Show log");
		
		this.textarea = new TextArea(7, 30);
		add(textarea);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent we) {
				menuItem.setSelected(false);
			}
		});
	}
	
	/**
	 * Returns the JCheckBoxMenuItem
	 * Used to toggle this button on windowClosing
	 * @author Kay van Bree
	 * @since 0.4
	 * @return Menu item
	 */
	public JCheckBoxMenuItem getMenuItem(){
		return menuItem;
	}
	
	/**
	 * Return the log
	 * This makes it Singeton!
	 * @author Kay van Bree
	 * @since 0.2
	 * @return Log the Log
	 */
	public static synchronized Log getLog(){
		if (log == null){
			log = new Log();
		}
		return log;	
	}
	
	/**
	 * No, you can't clone
	 * @author Kay van Bree
	 * @since 0.2
	 * @return Nothing hahaha!
	 * @throws CloneNotSupportedException 
	 */
	@Override
	public Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException(); 
	}
	
	/**
	 * Print something to the log window
	 * @author Kay van Bree
	 * @since 0.1
	 * @param text What we want to print
	 */
	public void print(String text){
		textarea.append(text);
		System.out.print(text);
	}
	
	/**
	 * Prints a line to the log window
	 * @author Kay van Bree
	 * @since 0.4
	 * @param text What we want to printa
	 */
	public void println(String text){
		textarea.append(text + "\n");
		System.out.print(text + "\n");
	}
}
